/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.client.mvp;

import com.google.gwt.place.shared.PlaceTokenizer;
import com.google.gwt.place.shared.Prefix;

/**
 * Form layout display. A place to add or edit a pojo. This place is toekend like:
 * #edit:pojoName:pojoId where pojoId is optional ( #edit:pojoName ) leading to a
 * new pojo form.
 * @author Nanjing RedOrange (www.red-orange.cn)
 */
public class EditPlace extends Place {

    final public Long id;
    public final static String prefix = "edit";
    public EditPlace(Class cls, Long id) {
        this(cls.getName(), id);
    }

    public EditPlace(String name, Long id) {
        super(name);
        this.id = id;
    }

    @Prefix(prefix)
    public static class Tokenizer implements PlaceTokenizer<EditPlace> {
        @Override
        public String getToken(EditPlace place) {
            return place.name + (place.id == null ? "" : ":" + place.id);
        }

        @Override
        public EditPlace getPlace(String token) {
            return new EditPlace(token.split(":")[0], token.split(":").length == 2 ? Long.valueOf(token.split(":")[1]) : null);
        }
    }

    /**
     * @return The url usable in the href attribute of an anchor.
     */
    @Override
    public String toString() {
        return "#" + prefix + ":" + name + (id == null ? "" : ":" + id);
    }
}